package com.scs.controller;

import com.scs.pojo.course;
import com.scs.pojo.relation;
import com.scs.pojo.student;
import com.scs.pojo.teacher;
import com.scs.service.courseService;
import com.scs.service.majorService;
import com.scs.service.relationService;
import com.alibaba.fastjson.JSONObject;
import com.scs.service.teacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/relation")
public class relationController {
    @Autowired
    private relationService relationService;
    @Autowired
    private courseService courseService;
    @Autowired
    private teacherService teacherService;

    @ResponseBody
    @RequestMapping(value = "/relationInsert",produces = "application/json;charset=utf-8")
    //关系表的插入操作
    public String relationInsert(HttpServletRequest request){
        JSONObject data = new JSONObject();
        //从前端获取的参数
        String courseId = request.getParameter("courseId");
        String teacherId = request.getParameter("teacherId");
        String classes = request.getParameter("classes");//班级名称
        List<course> course = courseService.getCourseById(courseId);
        //--------------------
        String courseName = course.get(0).getCourseName();//课程名称
        String collegeName = course.get(0).getCourseMajor();//学院名称
        //--------------------
        String majorName = course.get(0).getCourseMajor();//专业名称
        //--------------------
        List<teacher> teacher = teacherService.getTeacherById(teacherId);
        String teacherName = teacher.get(0).getRealName();
        //--------------------
        relation item = new relation("0",collegeName,majorName,
                classes,courseName,teacherId,teacherName);

        int count = relationService.relationInsert(item);
        if(count!=1){
            data.put("success",0);
            data.put("msg","数据插入失败");
        }else{
            data.put("success",1);
            data.put("msg","数据插入成功");
            ;        }
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getAllRelation",produces = "application/json;charset=utf-8")
    //获得所有关系数据
    public String getAllRelation(){
        JSONObject data = new JSONObject();
        data.put("code", 0);
        data.put("msg", "获取课程分配列表成功");
        List<relation> relationList = relationService.getAllRelation();
        data.put("data",relationList);
        data.put("count",relationList.size());
        return data.toJSONString();
    }


    @ResponseBody
    @RequestMapping(value = "/delRelation",produces = "application/json;charset=utf-8")
    //删除指定课程关联
    public String delRelation(HttpServletRequest request){
        String relationId = request.getParameter("relationId");
        JSONObject data = new JSONObject();
        if (relationId == null || relationId.equals("")){
            data.put("code", 110);
            data.put("msg", "删除课程关联失败");
            data.put("success",0);
        }else {
            int delNUm = relationService.delRelation(relationId);
            if (delNUm > 0){
                data.put("code", 0);
                data.put("msg", "删除课程关联成功");
                data.put("success",1);
            }else {
                data.put("code", 110);
                data.put("msg", "删除课程关联失败");
                data.put("success",0);
            }
        }
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getRelationByKey",produces = "application/json;charset=utf-8")
    //获得所有关系数据
    public String getRelationByKey(HttpServletRequest request){
        String key = request.getParameter("key");
        JSONObject data = new JSONObject();
        if (key == null || key.equals("")){
            List<relation> relationList = relationService.getAllRelation();
            data.put("code", 0);
            data.put("msg", "查询课程分配列表成功");
            data.put("data",relationList);
            data.put("count",relationList.size());
        }else {
            List<relation> relationList = relationService.getRelationByKey(key);
            data.put("code", 0);
            data.put("msg", "查询课程分配列表成功");
            data.put("data",relationList);
            data.put("count",relationList.size());
        }
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/batchDeleteRelation",produces = "application/json;charset=utf-8")
    //批量删除关系
    public String batchDeleteRelation(HttpServletRequest request){
        JSONObject data = new JSONObject();
        List<String> List = JSONObject.parseArray(request.getParameter("relationId"),String.class);
        data.put("code", 0);
        data.put("msg", "batchDeleteRelation");
        int count = relationService.batchDeleteRelation(List);
        if (count > 0) {
            data.put("success", "1");
            data.put("msg", "删除选中关系成功");
        } else {
            data.put("success", "0");
            data.put("msg", "删除选中关系失败");
        }
        data.put("count", count);
        data.put("data", null);
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/selectCourseByTeacherId",produces = "application/json;charset=utf-8")
    //通过teacherId获得课程信息
    public String selectCourseByTeacherId(HttpServletRequest request, HttpSession session){
        JSONObject data = new JSONObject();
        //teacher teacher = (com.scs.pojo.teacher) session.getAttribute("teacher");
        // String teacherId = teacher.getTeacherId();
        String teacherId = (String) request.getSession().getAttribute("userInformation");

        if (teacherId == null || teacherId.equals("")){
            data.put("success",0);
            data.put("msg","未获取到有效用户信息");
            //data.put("realName",teacher.getRealName());
            data.put("count",0);
            return data.toJSONString();
        }
        List<teacher> userData =(List<teacher>) request.getSession().getAttribute("userData");
        String teacherName = userData.get(0).getRealName();
        List<relation> relationList = relationService.selectRelationByTeacherId(teacherId);
        if(relationList.size()==0){
            data.put("success",0);
            data.put("msg","未查询到该教师与课程的关联");
            return data.toJSONString();
        }
        List<String> courseNameList = new ArrayList<>();
        for(relation item : relationList){
            String courseName = item.getCourseName();
            courseNameList.add(courseName);
        }
        List<course> courseList = courseService.batchSelectCourseByName(courseNameList);
        if (courseList.size()!=0){
            data.put("success",1);
            data.put("msg","查询课程成功");
            //data.put("realName",teacher.getRealName());
            data.put("data",courseList);
            data.put("teacherName", teacherName);
        }else
        {
            data.put("success",0);
            data.put("msg","查询课程失败");
            //data.put("realName",teacher.getRealName());
            data.put("count",0);
        }
        return data.toJSONString();

    }

    @ResponseBody
    @RequestMapping(value = "/selectCourseByClasses",produces = "application/json;charset=utf-8" )
    //获取该学生班级课程列表
    public String selectCourseByClasses(HttpServletRequest request){
        JSONObject data = new JSONObject();
        List<student> students = (List<student>) request.getSession().getAttribute("userData");
        if (students == null||students.size()==0){
            data.put("code",110);
            data.put("msg","您无权访问该接口");
            return data.toJSONString();
        }
        String classes = students.get(0).getClasses();
        if(classes == null || classes.equals("")){
            data.put("code",110);
            data.put("msg","未找到该学生所在的班级信息");
            return data.toJSONString();
        }
        List<relation> relationList = relationService.selectCourseByClasses(classes);
        if(relationList.size()==0){
            data.put("count",0);
            data.put("code",110);
            data.put("data",null);
            data.put("msg","未查询到该班级与课程的关联");
            return data.toJSONString();
        }
        List<String> courseNameList = new ArrayList<>();
        for(relation item : relationList){
            String courseName = item.getCourseName();
            courseNameList.add(courseName);
        }
        List<course> courseList = courseService.batchSelectCourseByName(courseNameList);
        if (courseList.size()!=0){
            data.put("count",courseList.size());
            data.put("code",0);
            data.put("msg","查询课程成功");
            //data.put("realName",teacher.getRealName());
            data.put("data",courseList);
        }else
        {
            data.put("code",110);
            data.put("data",null);
            data.put("msg","查询课程失败");
            //data.put("realName",teacher.getRealName());
            data.put("count",0);
        }
        return data.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "selectClassesByTeacherIdAndCourseName",produces = "application/json;charset=utf-8")
    //获取该教师该课程所教的班级
    public String selectClassesByTeacherIdAndCourseName(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String courseName = request.getParameter("courseName");
        String teacherId = (String) request.getSession().getAttribute("userInformation");
        if(teacherId == null || teacherId.equals("")){
            data.put("msg","未获取到有效用户");
            data.put("success",0);
            return data.toJSONString();
        }
        List<relation> relationList = relationService.selectClassesByTeacherIdAndCourseName(teacherId,courseName);
        if(relationList.size()==0){
            data.put("msg","查询失败");
            data.put("code",110);
            data.put("data",null);
            data.put("count",0);
        }else{
            data.put("msg","查询成功");
            data.put("code",0);
            data.put("data",relationList);
            data.put("count",relationList.size());
        }
        return data.toJSONString();
    }

}
